home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex1.c next >
C/C++ Source or Header  |  1995-04-20  |  3KB  |  85 lines

  1. #include <genstub.c>
  2.  
  3. // Child thread just loops until program exits.
  4. DWORD WINAPI ChildThreadProc( LPVOID lpNoData )
  5. {
  6.    MSG msg;
  7.    HWND hWnd = CreateWindow( lpszAppName, "Child Thread's Window",
  8.                              WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  9.                              CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL );
  10.    if ( !hWnd )
  11.       return( FALSE );
  12.  
  13.    ShowWindow( hWnd, SW_SHOW );
  14.    UpdateWindow( hWnd );
  15.  
  16.    while (GetMessage( &msg, NULL, 0, 0 ))
  17.    {
  18.       TranslateMessage( &msg );
  19.       DispatchMessage( &msg );
  20.    }
  21.    ExitThread( TRUE );
  22. }
  23.  
  24. // Windows message procedure for windows of both threads.
  25. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  26. {
  27.    static DWORD  dwChildThreadId = 0;    // child thread ID
  28.    static DWORD  dwParentThreadId = 0;   // parent thread ID
  29.    static HWND   hChildThreadWnd = 0;    // child thread's window
  30.    static HWND   hParentThreadWnd = 0;   // parent thread's window
  31.    static BOOL   fAttachedInput = FALSE; // status of attached input
  32.  
  33.    switch (uMsg)
  34.    {
  35.          case WM_CREATE:   // Set globals and create thread.
  36.                  if (hChildThread==0)
  37.                  {
  38.                     hParentThreadWnd = hWnd;
  39.                     dwParentThreadId = GetCurrentThreadId();
  40.                     // Create a thread
  41.                     CreateThread( NULL, 0, ChildThreadProc,
  42.                                   NULL, 0, &dwChildThreadId );
  43.                  }
  44.                  else
  45.                     hChildThreadWnd = hWnd;
  46.                  return DefWindowProc( hWnd, uMsg, wParam, lParam );
  47.          case WM_COMMAND:       // process menu items
  48.                  switch ( LOWORD( wParam )  )
  49.                  {
  50.                      case IDM_TEST:
  51.                      {
  52.                           HDC hDC = GetDC( hWnd );
  53.                           // Toggle attached thread input.
  54.                           if ( fAttachedInput )
  55.                              AttachThreadInput( dwChildThreadId,
  56.                                 dwParentThreadId, !fAttachedInput );
  57.                           else
  58.                              AttachThreadInput( dwChildThreadId,
  59.                                 dwParentThreadId, !fAttachedInput );
  60.                           fAttachedInput = !fAttachedInput;
  61.                           // Print Message.
  62.                           TextOut( hDC, 0, 0, fAttachedInput ?
  63.                                    "Attached  " : "Detached  ", 10);
  64.                           ReleaseDC( hWnd, hDC );
  65.                           // Set active window to opposite window.
  66.                           // Only succeeds if inputs are attached.
  67.                           if ( hWnd==hParentThreadWnd )
  68.                               SetActiveWindow( hChildThreadWnd );
  69.                           else  // Child thread's window
  70.                               SetActiveWindow( hParentThreadWnd );
  71.                      }
  72.                      break;
  73.                      case IDM_EXIT:
  74.                           DestroyWindow( hWnd );
  75.                           break;
  76.                  }
  77.                  break;
  78.          case WM_DESTROY: // only parent closes
  79.                  PostQuitMessage( 0 );
  80.                  break;
  81.          default:
  82.                  return DefWindowProc( hWnd, uMsg, wParam, lParam );
  83.    }
  84.    return (NULL);
  85. }